//
//  AbbBaseViewController.swift
//  AbbNavigationController
//
//  Created by abb on 16/7/31.
//  Copyright © 2016年 abb. All rights reserved.
//

import UIKit
enum AbbAction {
    case ViewAppear
    case ViewDisappear
}
/// 当前主题的是否透明
var s_appearanceBarTranslucent:Bool?
class AbbBaseViewController: UIViewController {
    /// 假navBar
    var bar:UINavigationBar?
    /// 是否使用透明navBar
    var userTransparentNavigationBar:Bool{
        return false
    }
    /**
     添加一个假navBar
     */
    func addFakeNavBar(action:AbbAction) {
        bar = AbbNavigationBar()
        // 判断是添加一个透明的假navBar还是不透明的
        if userTransparentNavigationBar {
            // 设置假navBar为透明
            bar!.shadowImage = UIImage()
            bar!.setBackgroundImage(UIImage(), forBarMetrics: .Default)
            bar!.translucent = true
        } else {
            // 设置假navBar为不透明
            bar!.setBackgroundImage(UINavigationBar.appearance().backgroundImageForBarMetrics(.Default), forBarMetrics: .Default)
            bar!.translucent = s_appearanceBarTranslucent!
        }
        bar!.barStyle = UINavigationBar.appearance().barStyle
        self.view.addSubview(bar!)
        bar!.frame = CGRect(x: 0, y: bar!.translucent ? 0 : (action == AbbAction.ViewAppear ? 0 : -64), width: UIScreen.mainScreen().bounds.size.width, height: 64)
    }
    /**
     删除假navBar
     */
    func removeFakeNavBar() {
        if (bar != nil) {
            bar!.removeFromSuperview()
        }
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // 禁止交互
        self.navigationController?.navigationBar.userInteractionEnabled = false
        // 根控制器进入时s_appearanceBarTranslucent为空,设置以记录主题默认的透明属性
        if nil == s_appearanceBarTranslucent {
            s_appearanceBarTranslucent = self.navigationController?.navigationBar.translucent
        }
        // 将默认navBar设置为透明
        self.navigationController?.navigationBar.translucent = true
        // 移除假navBar
        self.removeFakeNavBar()
        // 判断是否需要添加
        if (self.navigationController as! AbbNavigationController).shouldAddFakeNavigationBar! {
            self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
            self.addFakeNavBar(AbbAction.ViewAppear)
        }
        
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        // 移除假navBar
        self.removeFakeNavBar()
        // 判断是否需要添加
        if (self.navigationController as! AbbNavigationController).shouldAddFakeNavigationBar! {
            self.addFakeNavBar(AbbAction.ViewDisappear)
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        // 显示完成后允许交互
        self.navigationController?.navigationBar.userInteractionEnabled = true
        // 判断是否需要透明,再根据此给默认navBar赋不同的值
        if !userTransparentNavigationBar {
            self.navigationController?.navigationBar.barStyle = UINavigationBar.appearance().barStyle
            self.navigationController?.navigationBar.translucent = s_appearanceBarTranslucent!
            self.navigationController?.navigationBar.setBackgroundImage(UINavigationBar.appearance().backgroundImageForBarMetrics(.Default), forBarMetrics: .Default)
        } else {
//            self.navigationController?.navigationBar.userInteractionEnabled = false
            self.navigationController?.navigationBar.translucent = true
        }
        // 设置完默认navBar后移除假nav
        self.removeFakeNavBar()
    }


}
